﻿using System.Net.NetworkInformation;
using System.Windows;
using Microsoft.Live;
using Microsoft.Live.Controls;
using Microsoft.Phone.Controls;
using Notes.Helpers;

namespace Notes
{
    public partial class SyncPage : PhoneApplicationPage
    {

        public SyncPage()
        {
            InitializeComponent();
			btnSignin.ClientId = App.ClientId;
            App.SyncViewModel.SyncView = this;
        }

		protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
		{
			base.OnNavigatedTo(e);

			Deployment.Current.Dispatcher.BeginInvoke(delegate
			{
				if (!NetworkInterface.GetIsNetworkAvailable())
				{
                    Messenger.ShowMessage("Error","No network available.");
				}

				if (App.SkyDriveClient == null)
					btnSync.IsEnabled = false;
				else
					btnSync.IsEnabled = true;
			});
		}

		private void btnSignin_Click(object sender, RoutedEventArgs e)
		{
            if (NetworkInterface.GetIsNetworkAvailable())
            {
                btnSignin.IsEnabled = true;
            }
            else
            {
				Deployment.Current.Dispatcher.BeginInvoke(delegate
				{
                    Messenger.ShowMessage("Error", "No network available.");
                    btnSignin.IsEnabled = false;
                    btnSync.IsEnabled = false;
				});
			}
		}

		private void btnSignin_SessionChanged(object sender, LiveConnectSessionChangedEventArgs e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(delegate
            {
                if (NetworkInterface.GetIsNetworkAvailable())
                {
                    btnSignin.IsEnabled = true;
                    if (e.Session != null && e.Status == LiveConnectSessionStatus.Connected)
                    {
                        App.SkyDriveSession = e.Session;
                        App.SkyDriveClient = new LiveConnectClient(e.Session);

                        SigninTextBlock.Text = AppResources.strSignedIn;
                        btnSync.IsEnabled = true;
                    }
                    else
                    {
                        SigninTextBlock.Text = AppResources.strNotSignedIn;
                        btnSync.IsEnabled = false;
                    }
				}
                else
                {
                    Messenger.ShowMessage("Error", "No network available.");
                    btnSignin.IsEnabled = false;
                    btnSync.IsEnabled = false;
					SigninTextBlock.Text = AppResources.strNotSignedIn;
                }
            });
        }

        private void btnSync_Click(object sender, RoutedEventArgs e)
        {
			App.SyncViewModel.SyncNotes();
        }

        private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            if ((e.Orientation & PageOrientation.Portrait) == (PageOrientation.Portrait))
            {
                LayoutRoot.Margin = new Thickness(0, 32, 0, 72);
            }
            else
            {
                LayoutRoot.Margin = new Thickness(72, 0, 72, 0);
            }
        }

	}
}